<template>
  <div class="login-container">
    <!-- 帮助入口 -->
    <div class="help-link">
      <a href="#" @click="closeLogin">✕</a>
      <a href="#" @click="showHelp">帮助</a>
    </div>
    
    <!-- 主内容 -->
    <main class="login-main">
      <h1 class="title">欢迎登录美团外卖</h1>

      <!-- 手机号输入 -->
      <div class="input-group">
        <div class="phone-prefix">
          <span>+86</span>
          <span class="arrow">▼</span>
        </div>
        <input 
          type="tel"
          placeholder="请输入手机号"
          class="phone-input"
          maxlength="11"
          v-model="phoneNumber"
          @input="validatePhone"
          @focus="onInputFocus"
          @blur="onInputBlur"
        >
        <div v-if="phoneError" class="error-message">{{ phoneError }}</div>
      </div>
      
      <!-- 密码输入 -->
      <div class="input-group">
        <input 
          :type="isPasswordVisible ? 'text' : 'password'"
          placeholder="请输入密码"
          class="phone-input"
          v-model="password"
          @input="validatePassword"
          @blur="validatePassword"
        >
        <i 
          class="eye-icon"
          :class="{ 'eye-open': isPasswordVisible }"
          @click="togglePasswordVisibility"
        ></i>
        <div v-if="passwordError" class="error-tip">{{ passwordError }}</div>
      </div>

      <!-- 协议勾选 -->
      <div class="agreement">
        <label class="checkbox-container" @click.stop>
          <input type="checkbox" v-model="isAgreementChecked">
          <span class="checkmark"></span>
        </label>
        <div class="agreement-text">
          <span>我已阅读并同意</span>
          <a href="#" @click="showUserAgreement">《用户协议》</a><span>、</span>
          <a href="#" @click="showPrivacyPolicy">《隐私政策》</a>
          <span>，并授权美团外卖使用该美团账号信息（如昵称、头像、收货地址）进行统一管理。</span>
        </div>
      </div>

      <!-- 登录按钮 -->
      <button 
        class="verify-btn" 
        :disabled="!isFormValid"
        @click="login"
      >
        {{ buttonText }}
      </button>

      <!-- 底部导航 -->
      <div class="footer-links">
        <a href="#" @click="switchToPasswordLogin">验证码登录</a>
        <a href="#" @click="showHelp">遇到问题</a>
      </div>
    </main>
    
    <!-- 底部第三方登录 -->
    <div class="bottom-container">
      <div class="logo">
        <a href="#" class="logo-item" @click="loginWithWeChat">
          <img src="@/assets/images/wx.png" alt="微信登录">
          <span>微信登录</span>
        </a>
        <a href="#" class="logo-item" @click="loginWithQQ">
          <img src="@/assets/images/qq.png" alt="QQ登录">
          <span>QQ登录</span>
        </a>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'LoginPage',
  data() {
    return {
      phoneNumber: '',
      password: '',
      isPasswordVisible: false,
      isAgreementChecked: false,
      phoneError: '',
      passwordError: '',
      isInputFocused: false,
      countdown: 0,
      buttonText: '登录'
    }
  },
  computed: {
    // 表单是否有效（手机号格式正确、密码不为空且已勾选协议）
    isFormValid() {
      return this.validatePhone() && this.validatePassword() && this.isAgreementChecked;
    }
  },
  methods: {
    // 手机号格式验证
    validatePhone() {
      const phoneRegex = /^1[3-9]\d{9}$/;
      
      if (!this.phoneNumber) {
        this.phoneError = '请输入手机号';
        return false;
      } else if (!phoneRegex.test(this.phoneNumber)) {
        this.phoneError = '请输入正确的手机号';
        return false;
      } else {
        this.phoneError = '';
        return true;
      }
    },
    
    // 密码验证
    validatePassword() {
      if (!this.password) {
        this.passwordError = '请输入密码';
        return false;
      } else if (this.password.length < 6) {
        this.passwordError = '密码长度不能少于6位';
        return false;
      } else {
        this.passwordError = '';
        return true;
      }
    },
    
    // 切换密码可见性
    togglePasswordVisibility() {
      this.isPasswordVisible = !this.isPasswordVisible;
    },
    
    // 输入框聚焦时
    onInputFocus() {
      this.isInputFocused = true;
    },
    
    // 输入框失焦时
    onInputBlur() {
      this.isInputFocused = false;
      // 失焦时再次验证
      this.validatePhone();
      this.validatePassword();
    },
    
    // 登录方法
    login() {
      // 表单验证
      if (!this.isFormValid) {
        return;
      }
      
      // 模拟登录请求
      this.buttonText = '登录中...';
      
      // 模拟API请求延迟
      setTimeout(() => {
        // 登录成功后的跳转逻辑
        console.log('登录成功，跳转到首页');
        
        // 使用Vue Router进行页面跳转
        if (this.$router) {
          this.$router.push('/home'); // 跳转到首页
        } else {
          // 如果没有路由实例，使用原生方法跳转
          window.location.href = '/home';
        }
        
        // 重置按钮文本
        this.buttonText = '登录';
      }, 1000);
    },
    
    // 其他功能函数（实际项目中需实现具体逻辑）
    closeLogin() {
      console.log('关闭登录页');
    },
    showHelp() {
      console.log('显示帮助中心');
    },
    showUserAgreement() {
      console.log('显示用户协议');
    },
    showPrivacyPolicy() {
      console.log('显示隐私政策');
    },
    switchToPasswordLogin() {
      console.log('切换到密码登录');
    },
    loginWithWeChat() {
      console.log('微信登录');
    },
    loginWithQQ() {
      console.log('QQ登录');
    }
  }
}
</script>

<style lang="less" scoped>
.login-container {
  width: 750px;
  margin: 0 auto;
  min-height: 100vh;
  background: #fff;
  padding: 40px 32px;
  box-sizing: border-box;
  position: relative;
  font-family: 'PingFang SC', 'Helvetica Neue', Arial, sans-serif;
  
  // 帮助入口样式
  .help-link {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 60px;
    
    a {
      text-decoration: none;
    }
    
    a:first-child {
      font-size: 50px;
      line-height: 1;
      color: #333;
    }
    
    a:last-child {
      font-size: 28px;
      color: #999;
    }
  }

  // 主内容区域样式
  .login-main {
    margin-top: 20px;
    
    // 标题
    .title {
      font-size: 48px;
      color: #333;
      text-align: center;
      margin-bottom: 80px;
      font-weight: 500;
    }
    
    // 手机号输入组
    .input-group {
      display: flex;
      align-items: center;
      border-bottom: 1px solid #eee;
      padding-bottom: 20px;
      margin-bottom: 30px;
      position: relative;
      
      .phone-prefix {
        display: flex;
        align-items: center;
        margin-right: 20px;
        
        span {
          font-size: 36px;
          color: #333;
        }
        
        .arrow {
          font-size: 24px;
          color: #666;
          margin-left: 10px;
        }
      }
      
      .phone-input {
        flex: 1;
        border: none;
        font-size: 36px;
        color: #333;
        padding: 20px 0;
        background: transparent;
        
        &::placeholder {
          color: #ccc;
        }
        
        &:focus {
          outline: none;
        }
      }
      
      // 错误提示
      .error-message, .error-tip {
        position: absolute;
        bottom: -30px;
        left: 0;
        font-size: 24px;
        color: #f53f3f;
      }
      
      // 密码可见性图标
      .eye-icon {
        position: absolute;
        right: 0;
        font-size: 32px;
        color: #999;
        cursor: pointer;
        
        &.eye-open::before {
          content: '';
        }
        
        &::before {
          content: '';
        }
      }
    }
    
    // 协议区域
    .agreement {
      display: flex;
      align-items: flex-start;
      margin-bottom: 60px;
      
      .checkbox-container {
        flex-shrink: 0;
        display: inline-block;
        position: relative;
        margin-right: 10px;
        cursor: pointer;
      }
      
      .agreement-text {
        margin: 0;
        font-size: 24px;
        line-height: 1.5em;
        color: #666;
        word-break: keep-all;
        hyphens: auto;
        text-align: justify;
        text-justify: inter-ideograph;
        
        a {
          color: #06f;
          text-decoration: none;
          white-space: nowrap;
        }
        
        > span:not(:last-child) {
          margin-right: 0.2em;
        }
      }
    }
    
    // 复选框样式
    input[type="checkbox"] {
      position: absolute;
      opacity: 0;
      cursor: pointer;
      height: 0;
      width: 0;
      z-index: 1;
      
      &:focus + .checkmark {
        box-shadow: 0 0 0 2px rgba(6, 153, 255, 0.2);
      }
    }
    
    .checkmark {
      position: relative;
      display: inline-block;
      width: 32px;
      height: 32px;
      border: 1px solid #ddd;
      border-radius: 4px;
      transition: all 0.2s;
      top: 2px;
      
      &::after {
        content: '';
        position: absolute;
        left: 10px;
        top: 6px;
        width: 8px;
        height: 14px;
        border: solid white;
        border-width: 0 2px 2px 0;
        transform: rotate(45deg);
        display: none;
        transition: all 0.2s;
      }
    }
    
    input[type="checkbox"]:checked + .checkmark {
      border-color: yellow;
      background-color: yellow;
    }
    
    input[type="checkbox"]:checked + .checkmark::after {
      display: block;
    }
    
    .checkbox-container:hover .checkmark {
      border-color: #ccc;
    }
    
    // 登录按钮
    .verify-btn {
      width: 100%;
      height: 88px;
      background: #ffd600;
      border: none;
      border-radius: 44px;
      font-size: 32px;
      color: #333;
      margin-bottom: 40px;
      cursor: pointer;
      transition: all 0.2s;
      font-weight: 500;
      
      &:disabled {
        background: #f5f5f5;
        color: #999;
        cursor: not-allowed;
      }
      
      &:active:not(:disabled) {
        transform: scale(0.98);
      }
    }
    
    // 底部链接
    .footer-links {
      display: flex;
      justify-content: space-between;
      
      a {
        font-size: 28px;
        text-decoration: none;
        color: #333;
        transition: color 0.2s;
        
        &:hover {
          color: #06f;
        }
      }
    }
  }
  
  // 底部容器
  .bottom-container {
    margin-top: auto;
    padding: 60% 0 40px;
    text-align: center;
  }
  
  // 第三方登录图标
  .logo {
    display: flex;
    justify-content: center;
    gap: 100px;
    
    &-item {
      display: flex;
      flex-direction: column;
      align-items: center;
      text-decoration: none;
      
      img {
        width: 65px;
        height: 60px;
        margin-bottom: 15px;
      }
      
      span {
        font-size: 28px;
        color: #666;
      }
    }
  }
}
</style>  